#' The application server-side
#'
#' @param input,output,session Internal parameters for {shiny}.
#'     DO NOT REMOVE.
#' @import shiny
#' @noRd
app_server <- function(input, output, session) {
  # Your application server logic
  db_table = database(all = TRUE)[order(-year, id)]
  db_show = db_table[, c("cancer_type", "name", "dataset.name", "dataset.data_platform", "dataset.data_type", "dataset.n", "dataset.tags", "year")]
  colnames(db_show) = c("Type", "Cohort", "Dataset", "Platform", "Data Type", "N", "Tags", "Year")

  db_selected <- reactive(getReactableState("db", "selected"))
  output$db <- renderReactable({
    reactable(
      db_show,
      columns = list(
        Cohort = reactable::colDef(minWidth = 350),
        Dataset = reactable::colDef(minWidth = 200),
        `Data Type` = reactable::colDef(minWidth = 120),
        N = reactable::colDef(maxWidth = 50),
        Year = reactable::colDef(maxWidth = 50)
      ),
      paginationType = "jump", showPageSizeOptions = TRUE,
      defaultPageSize = 20, pageSizeOptions = c(5, 10, 20, 50, 100),
      selection = "multiple", onClick = "select", resizable = TRUE,
      class = "db-table",
      defaultColDef = reactable::colDef(headerClass = "db-header"),
      rowStyle = list(cursor = "pointer"),
      searchable = TRUE, striped = FALSE, highlight = TRUE,
      wrap = FALSE, compact = TRUE, bordered = TRUE,
      theme = reactableTheme(
        borderColor = "#dfe2e5",
        stripedColor = "#f6f8fa",
        highlightColor = "#f0f5f9",
        cellPadding = "8px 12px",
        style = list(fontFamily = "-apple-system, BlinkMacSystemFont, Segoe UI, Helvetica, Arial, sans-serif",
                     fontSize = 12),
        searchInputStyle = list(width = "100%")
      ),
      details = row_details(db_table)
    )
  })

  output$db_selected <- renderPrint({
    print(db_selected())
  })

}
